﻿using CastingWellEquipmentGUI.Config;
using CastingWellEquipmentGUI.ViewModel.Common.Command;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace CastingWellEquipmentGUI.ViewModel.Modal.EditCastingNoPanel
{
    public class EditCastingNoPanelViewModel : ViewModelBase
    {
        /// <summary>
        /// 确认命令
        /// </summary>
        public DelegateCommand ConfirmAndHideEditCastingNoPanelWindowCommand { set; get; }
        /// <summary>
        /// 关闭本窗体命令
        /// </summary>
        public DelegateCommand HideEditCastingNoPanelWindowCommand { set; get; }
        /// <summary>
        /// 格式提示
        /// </summary>
        private string _FormatTips;
        /// <summary>
        /// 格式提示
        /// </summary>
        public string FormatTips
        {
            get { return _FormatTips; }
            set
            {
                _FormatTips = value;
                RaisePropertyChanged("FormatTips");
            }
        }
        /// <summary>
        /// 格式提示边框颜色
        /// </summary>
        private System.Windows.Media.Brush _FormatTipsBorderBrush;
        /// <summary>
        /// 格式提示边框颜色
        /// </summary>
        public System.Windows.Media.Brush FormatTipsBorderBrush
        {
            get { return _FormatTipsBorderBrush; }
            set
            {
                _FormatTipsBorderBrush = value;
                RaisePropertyChanged("FormatTipsBorderBrush");
            }
        }
        /// <summary>
        /// 格式提示字体颜色
        /// </summary>
        private System.Windows.Media.Brush _FormatTipsForeground;
        /// <summary>
        /// 格式提示字体颜色
        /// </summary>
        public System.Windows.Media.Brush FormatTipsForeground
        {
            get { return _FormatTipsForeground; }
            set
            {
                _FormatTipsForeground = value;
                RaisePropertyChanged("FormatTipsForeground");
            }
        }

        /// <summary>
        /// 格式提示背景颜色
        /// </summary>
        private System.Windows.Media.Brush _FormatTipsBackground;
        /// <summary>
        /// 格式提示背景颜色
        /// </summary>
        public System.Windows.Media.Brush FormatTipsBackground
        {
            get { return _FormatTipsBackground; }
            set
            {
                _FormatTipsBackground = value;
                RaisePropertyChanged("FormatTipsBackground");
            }
        }


        /// <summary>
        /// 铸造编号
        /// </summary>
        private string _CastingNo;
        /// <summary>
        /// 铸造编号
        /// </summary>
        public string CastingNo
        {
            get { return _CastingNo; }
            set
            {
                _CastingNo = value;
                RaisePropertyChanged("MonitoreTags");
            }
        }

        /// <summary>
        /// 本ViewModel处理器
        /// </summary>
        public EditCastingNoPanelViewModel()
        {
            //初始化命令
            InitCommand();
            Messenger.Default.Register<string>(this, "VerificationCastingNo", (message) =>
            {
                ValidationFunc(message);
            });
        }

        /// <summary>
        /// 初始化命令方法
        /// </summary>
        private void InitCommand()
        {
            ConfirmAndHideEditCastingNoPanelWindowCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(ConfirmAndHideEditCastingNoPanelWindowHandler)
            };
            HideEditCastingNoPanelWindowCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(HideEditCastingNoPanelWindowHandler)
            };
        }
        /// <summary>
        /// 关闭本窗体命令处理器
        /// </summary>
        /// <param name="obj"></param>
        private void HideEditCastingNoPanelWindowHandler(object obj)
        {
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "HideEditCastingNoPanel"));
            FormatTips = "";
            FormatTipsBorderBrush = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#00FFFFFF"));
            FormatTipsForeground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#00FFFFFF"));
            FormatTipsBackground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#00FFFFFF"));
        }
        /// <summary>
        /// 确认命令处理器
        /// </summary>
        /// <param name="obj"></param>
        private void ConfirmAndHideEditCastingNoPanelWindowHandler(object obj)
        {
            if (CastingNo != null && CastingNo.Trim().Length > 0)
            {
                if (ValidationFunc(CastingNo))
                {
                    SystemConfig.SetCastingNo(CastingNo);
                    Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "HideEditCastingNoPanel"));
                    Messenger.Default.Send<string>(CastingNo, "PreCheckPageCastingNo");
                    FormatTips = "";
                    FormatTipsBorderBrush = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#00FFFFFF"));
                    FormatTipsForeground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#00FFFFFF"));
                    FormatTipsBackground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#00FFFFFF"));
                }
            }
            else
            {
                Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "HideEditCastingNoPanel"));
            }
        }

        /// <summary>
        /// 验证方法
        /// </summary>
        /// <returns></returns>
        private bool ValidationFunc(string str)
        {
            string[] splitStr = str.Split(' ');
            bool result;
            if (splitStr.Length == 4)
            {
                if (splitStr[0].Length > 0 &&
                    splitStr[1].Length > 0 &&
                    splitStr[2].Length > 0 &&
                    splitStr[3].Length > 0)
                {
                    result = true;
                    FormatValidationContentConfig("通过");
                }
                else
                {
                    result = false;
                    FormatValidationContentConfig("不通过");
                }
            }
            else
            {
                result = false;
                FormatValidationContentConfig("不通过");
            }
            return result;
        }

        /// <summary>
        /// 验证结果设置UI效果和错误提示
        /// </summary>
        /// <param name="result">验证结果</param>
        private void FormatValidationContentConfig(string result)
        {
            FormatTipsBackground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FFFFFF"));
            if (result == "通过")
            {
                FormatTips = "格式正确，验证通过。";
                FormatTipsBorderBrush = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#16D46B"));
                FormatTipsForeground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#16D46B"));

            }
            else if (result == "不通过")
            {
                FormatTips = "格式不正确，验证不通过。正确格式为\"**** **** **** ****\",\"*\"代表任意字符。";
                FormatTipsBorderBrush = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF8642"));
                FormatTipsForeground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF8642"));
            }
        }
    }
}
